﻿/*!
 * One JavaScript Library v1.0.0
 * Date: 2012-02-14 10:41:11 
 * Copyright (c) 2009 - 2012 By Li Hui Gang
 */
 
(function(factory) {
  if (typeof define === 'function') {
    define('#onemod/onecore.js', [], factory);
  }
  else {
    factory();
  }

})(function(require) {
	//一下为插件内容

;(function( window, undefined ){

var one = function( selector, context )
    {
	    return new one.fn.init( selector, context );
	},
	
	document = window.document,
	
	push = Array.prototype.push,
	toString = Object.prototype.toString,
	
	// event
	readyBound = false,
	readyList = [],
	DOMContentLoaded,
	rootDocument,
	
	// style
    isOpacity = 'opacity' in document.documentElement.style,
    defaultView = document.defaultView || {},
	rspec = /href|src|style/,
	
	rclass = /[\n\t]/g,
	expando = 'ONE' + (new Date).getTime();

one.fn = one.prototype =
{
    constructor: one,
	
	init: function( selector, context )
	{
	    var that = this, name, ret;
		
		if( !selector ) return that;
		
		if( selector.nodeType || one.isWindow(selector) )
		{
		    that[0] = selector;
			that.length = 1; return that;
		}
		
		context = context || document;
		
		if( typeof selector === 'string' )
		{
		    if( selector.charAt(0) === '#' )
			{
			    name = selector.slice(1);
				ret = context.getElementById(name);
				
				if( ret )
				{
				    that.length = 1; that[0] = ret;
				}
				
				return that;
			}
			else if( /^\w+$/.test(selector) )
		        ret = toArray( context.getElementsByTagName(selector) );
			else if( selector.charAt(0) === '.' )
			{
			    name = selector.slice(1);
				ret = getElementsByClassName(name,context);
			}
		}
		else if( typeof selector === 'function' )
		    return rootDocument.ready( selector );
		
		return that.setArray( ret );
	},
	
	setArray: function( elems )
	{
	    this.length = 0;
		push.apply( this, elems );
		return this;
	},
	
	bind: function( type, fn )
	{
		for( var i = 0, l = this.length; i < l; i++ )
		    one.event.add( this[i], type, fn );
		return this;
	},
	
	unbind: function( type, fn )
	{
		for( var i = 0, l = this.length; i < l; i++ )
		    one.event.remove( this[i], type, fn );
		return this;
	},
	
	html: function( value )
	{
	    var that = this, i = 0, l = that.length;
		
		if( value === undefined )
		    return that[0] ? that[0].innerHTML : null;
		
		for( ; i < l; i++ )
		{
		    cleanData( that[i].getElementsByTagName('*') );
			that[i].innerHTML = value;
		}
		
		return that;
	},
	
	remove: function()
	{
		var i = 0, elem;
		
		for( ; (elem = this[i]) !== undefined; i++ )
		{
		    cleanData( elem.getElementsByTagName('*') );
			cleanData( [elem] );
			
			if( elem.parentNode )
			    elem.parentNode.removeChild(elem);
		}

		return this;
	},
	
	hasClass: function( selector )
	{
		var className = ' ' + selector + ' ';
		for( var i = 0, l = this.length; i < l; i++ )
		{
			if( (' ' + this[i].className + ' ').replace(rclass, ' ').indexOf( className ) > -1 )
				return true;
		}

		return false;
	},
	
	addClass: function( value )
	{
		var that = this, i = 0, l = that.length;
		
		for( ; i < l; i++ )
		{
			if( !that[i].className )
			    that[i].className = value;
			else
			{
			    if( (' ' + that[i].className + ' ').indexOf( ' ' + value + ' ' ) < 0 )
			        that[i].className += ' ' + value;
			}
		}
		
		return this;
	},
	
	removeClass: function( value )
	{
		var that = this, i = 0, l = that.length, className;
		
		for( ; i < l; i++ )
		{
			if( value )
			{
				className = (' ' + that[i].className + ' ').replace(rclass, ' ').replace(' ' + value + ' ', ' ');
				that[i].className = className.substring( 1, className.length - 1 );
			}
			else
			    that[i].className = '';
		}
		
		return that;
	},
	
	css: function( key, value )
	{
	    return access( this, key, value, 'Style' );
	},
	
	attr: function( key, value )
	{
        return access( this, key, value, 'Attr' );
	},
	
	ready: function( fn )
	{
	    one.bindReady();
		
		if( one.isReady )
		    fn.call( document, one );
		else if( readyList )
		    readyList.push( fn );
			
		return this;
	},
	
	offset: function()
	{
		if( !this[0] ) return null;
		
		var elem = this[0],
		    box = elem.getBoundingClientRect(),
			doc = elem.ownerDocument.documentElement,
			body = elem.ownerDocument.body,
			
			clientTop = doc.clientTop || body.clientTop || 0,
			clientLeft = doc.clientLeft || body.clientLeft || 0,
			
			top = box.top + ( self.pageYOffset || doc.scrollTop || body.scrollTop ) - clientTop,
			left = box.left + ( self.pageXOffset || doc.scrollLeft || body.scrollLeft ) - clientLeft;
		
		return { 'top': top, 'left': left };
	}
};

one.fn.init.prototype = one.fn;
	
function toArray( array )
{
    var ret = [], i = array.length;
	
	if( i !== 0 )
	{
	    while(i)
		    ret[--i] = array[i];
	}
	
	return ret;
};
	
function getElementsByClassName( name, context )
{
	if( document.getElementsByClassName )
	    return toArray( context.getElementsByClassName(name) );
		
	name = ' ' + name + ' ';
	var nodes = context.getElementsByTagName('*'),
		i = 0, idx = 0,
		node, sets = [];
	while( node = nodes[i++] )
	{
		if( (' ' + node.className + ' ').indexOf(name) !== -1 )
			sets[idx++] = node;
	}
	return sets = idx ? sets : [];
};

one.extend = function( object, target )
{
    for( var name in target )
	{
	    if( object[name] === undefined ) object[name] = target[name];
	}
	
	return object;
};

// 一些小的工具函数
one.extend( one, {
	isFunction: function( obj )
	{
	    return toString.call(obj) === '[object Function]';
	},
	
	isArray: function( obj )
	{
	    return toString.call(obj) === '[object Array]';
	},
	
	isWindow: function( obj )
	{
	    return obj && typeof obj === 'object' && 'setInterval' in obj;
	},
	
	trim: function( text )
	{
	    return (text || '').replace(/^\s+/, '').replace(/\s+$/, '');
	},
	
	nodeName: function( elem, name )
	{
		return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
	},
	
	each: function( object, callback )
	{
	    var name, i = 0, length = object.length;
		
		if( length === undefined )
		{
		    for( name in object )
			    if( callback.call( object[name], name, object[name] ) === false )
				    break;
		}
		else
		    for( var value = object[0];
			    i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}
		
		return object;
	}
});

/*! 事件绑定部分代码开始，包括DOM准备就绪部分 */
one.extend( one, {
    cache: {
	    guid: 0, fuid: 0,
		
	    func: function(uid)
	    {
	        return function(){
		        try{
			        one.event.handle.apply( one.cache[uid].elem, arguments );
			    }catch(e){};
		    };
		}
	},
	
	event: {
	    add: function( elem, type, handler )
		{
			var guid = elem[expando] || (elem[expando] = ++one.cache.guid);
			
			if( !one.cache[guid] )
			{
				one.cache[guid] = {
					elem: elem,
					listener: one.cache.func(guid),
					events: {}
				};
			}
			
			if( type && !one.cache[guid].events[type] )
			{
				one.cache[guid].events[type] = [];
				
				elem.addEventListener
				? elem.addEventListener( type, one.cache[guid].listener, false )
				: elem.attachEvent( 'on' + type, one.cache[guid].listener );
			}
			
			if( handler )
			{
				if( !handler.fuid )
					handler.fuid = ++one.cache.fuid;
				one.cache[guid].events[type].push( handler );
			}
			
			elem = null;
		},
	
		remove: function( elem, type, handler )
		{
			var handlers, events, ret, eventType, handlerObj;
			
			try{
				handlers = one.cache[ elem[expando] ];
			}catch(e){ return; };
			
			if( !handlers ) return;
			events = handlers.events;
			
			if( type === undefined )
			{
				for( var type in events )
					one.event.remove( elem, type );
				return;
			}
			
			eventType = events[type];
			
			if( eventType )
			{
				if( handler )
				{
					for( var i = 0; i < eventType.length; i++ )
					{
						handlerObj = eventType[i];
						if( handler.fuid === handlerObj.fuid )
							eventType.splice( i--, 1 );
					}
				}
				else
				{
					for( var i = 0; i < eventType.length; i++ )
						eventType.splice( i--, 1 );
				}
				
				for( ret in events[type] ) break;
				if( !ret )
				{
					elem.removeEventListener
					? elem.removeEventListener( type, handlers.listener, false )
					: elem.detachEvent( 'on' + type, handlers.listener );
					
					ret = null;
					delete events[type];
				}
			}
			
			for( ret in events ) break;
			if( !ret )
			{
				handlers.elem = null;
				delete one.cache[ elem[expando] ];
				
				try{ delete elem[expando]; }catch(e)
				{
			        if( elem.removeAttribute )
				        elem.removeAttribute(expando);
				};
			}
		},
	
		handle: function(event)
		{
			event = one.event.fix( event || window.event );
			
			if( !event.currentTarget )
				event.currentTarget = this;
			
			var handlers = one.cache[this[expando]].events[event.type].slice(0),
				i = 0, l = handlers.length, handleObj;
			
			for( ; i < l; i++ )
			{
				handleObj = handlers[i];
				
				if( handleObj.call(this, event) === false )
				{
					event.preventDefault();
					event.stopPropagation();
				}
			}
		},
		
		fix: function(event)
		{
			if( event.target ) return event;
			
			event.preventDefault = preventDefault;
			event.stopPropagation = stopPropagation;
			event.target = event.srcElement || document;
			
			return event;
		}
	},
	
	isReady: false,
	
	ready: function()
	{
	    if( !one.isReady )
		{
		    if( !document.body )
			    return setTimeout( one.ready, 13 );
			
			one.isReady = true;
			
			if( readyList )
			{
			    var fn, i = 0;
				while( (fn = readyList[ i++ ]) )
				    fn.call( document, one );
				readyList = null;
			}
		}
	},
	
	bindReady: function()
	{
	    if( readyBound ) return;
		
		readyBound = true;
		
		if( document.readyState === 'complete' )
		    return one.ready();
			
		if( document.addEventListener )
		{
		    document.addEventListener( 'DOMContentLoaded', DOMContentLoaded, false );
			window.addEventListener( 'load', one.ready, false );
		}
		else if( document.attachEvent )
		{
		    document.attachEvent( 'onreadystatechange', DOMContentLoaded );
			window.attachEvent( 'onload', one.ready );
			
			var toplevel = false;
			
			try{
				toplevel = window.frameElement == null;
			}catch(e){};
			
			if( document.documentElement.doScroll && toplevel )
			    doScrollCheck();
		}
	}
});

function preventDefault()
{
    this.returnValue = false;
};

function stopPropagation()
{
    this.cancelBubble = true;
};

function cleanData( elems )
{
    var i = 0, l = elems.length;
	
	for( ; i < l; i++ )
	    one.event.remove( elems[i] );
};

function doScrollCheck()
{
	if( one.isReady ) return;

	try{
		document.documentElement.doScroll('left');
	}catch(e){
		setTimeout( doScrollCheck, 1 );
		return;
	};

	one.ready();
};

if( document.addEventListener )
{
	DOMContentLoaded = function(){
		document.removeEventListener( 'DOMContentLoaded', DOMContentLoaded, false );
		one.ready();
	};
}
else if( document.attachEvent )
{
	DOMContentLoaded = function(){
		if( document.readyState === 'complete' ){
			document.detachEvent( 'onreadystatechange', DOMContentLoaded );
			one.ready();
		}
	};
}

if( window.ActiveXObject )
{
    window.attachEvent('onunload', function(){
	    for( var id in one.cache )
		{
		    try{
			    one.event.remove( one.cache[id].elem );
			}catch(e){};
		}
	});
}

rootDocument = one(document);
/*! 事件绑定部分代码结束，包括DOM准备就绪部分 */

/*! 操作DOM元素的属性、样式代码部分开始 */
function access( elems, key, value, type )
{
    var options = key, i = 0, l = elems.length;
	
	if( typeof key === 'string' )
	{
		if( value === undefined )
			return l ? one['get'+type]( elems[0], key ) : null;
		else
		{
		    options = {};
			options[key] = value;
		}
	}
	
	for( ; i < l; i++ )
		one[type]( elems[i], options );
	
	return elems;
};

one.extend( one, {
    Attr: function( elem, obj )
	{
		for( var key in obj )
		{
		    if( !rspec.test(key) )
			    elem[key] = obj[key];
			else
			    elem.setAttribute( key, '' + obj[key] );
		}
	},
	
	getAttr: function( elem, name )
	{
	    var special = rspec.test(name), attr;
		
		if( !special )
		    return elem[name];
		else
		{
		    attr = window.ActiveXObject && special
			    ? elem.getAttribute( name, 2 )
				: elem.getAttribute( name );
			return attr === null ? undefined : attr;
		}
	},
	
    Style: function( elem, obj )
	{
	    var style = elem.style;
		
		for( var key in obj )
		{
		    if( key === 'opacity' && !isOpacity )
		        style.filter = (style.filter||'').replace( /alpha\([^)]*\)/, '' ) +
			        (parseInt( obj[key] ) + '' == 'NaN' ? '' : 'alpha(opacity=' + obj[key] * 100 + ')');
		    else
			    style[key] = obj[key];
		}
	},
	
	getStyle: function( elem, name )
	{
	    var ret, style = elem.style;
		
		if( style && style[name] )
		    ret = style[name];
		else if( defaultView.getComputedStyle )
			ret = defaultView.getComputedStyle(elem,false)[name];
		else if( elem.currentStyle )
			ret = elem.currentStyle[ name ] || '';
		
		return ret;
	}
});
/*! 操作DOM元素的属性、样式代码部分结束 */

/*! 获取窗口或文档尺寸以及滚动条位置-[只读,不支持写入]-代码部分开始 */
one.each(['scrollLeft','scrollTop'],function(i,name){
    one.fn[name] = function()
	{
	    var elem = this[0],
		    win = getWindow(elem);
		
		return win ? 
			('pageXOffset' in win) ?
				win[i ? 'pageYOffset' : 'pageXOffset'] :
				win.document.documentElement[name] || win.document.body[name] :
			elem[name];
	};
});

function getWindow( elem )
{
	return one.isWindow( elem ) ?
		elem : elem.nodeType === 9 ?
			elem.defaultView || elem.parentWindow :
		false;
};

one.each(['Height','Width'],function(i,name){
    var type = name.toLowerCase();
	
	one.fn[type] = function()
	{
	    var elem = this[0];
		
		return one.isWindow(elem) ?
		    elem.document.documentElement['client' + name] || elem.document.body['client' + name] :
			(elem.nodeType === 9) ?
			    Math.max(
				    elem.documentElement['client' + name],
					elem.body['scroll' + name], elem.documentElement['scroll' + name],
					elem.body['offset' + name], elem.documentElement['offset' + name]
				) : null;
	};
});
/*! 获取窗口或文档尺寸以及滚动条位置-[只读,不支持写入] 代码部分结束 */

window.one = window.$ === undefined ? window.$ = one : window.J = one;

})( window );
////
	if(require) return window.one;
});